/*
*
* 沃库工业网H5搜索筛选组件公共方法
*
* */

// 项目信息筛选
import { getVmProjectData } from '@/api/project.js'
import { getVmIndexRequireData } from '@/api/index.js'
import { source_money_ratio_APP } from '@/api/statistics_analysis'
import { getCurMonth, getCountDays } from '@/utils/vm-common-getTime.js'

export class CommonSearchPopup {
  constructor(_this, _type) {
    this.that = _this;
    this.companyName = this.that.companyName;
    this.startDay = null;
    this.endDay = null;

    /* 项目数据 */
    this.industry = new Array(); // 行业
    this.stage = new Array(); // 阶段
    this.datetime = new Array(); // 最近日期
    this.companyType = new Array(); // 发布
    /* 需求数据 */
    this.type = new Array(); // 采购方式
    this.state = new Array(); // 阶段
    this._type = _type; // 项目筛选，判断是首页的项目筛选还是我的制单的项目筛选
    this.init();

    /* 统计分析 -> 询源分析功能 */
    this.month = null;

    /* 统计分析 -> 出库筛选数据 */
    this.tempoutStoreIndex = 0; // 出库仓库临时存储index
    this.outStoreData = new Array(); // 出库depID
  }
  // 初始化
  init() {
    // 打开模态框
    this.that.popupVisible = !this.that.popupVisible;

    // 初始化统计分析询源分析主体入口
    switch (this.that.type) {
      case "sourcePage":
        this.ActiveByCurrentMouth();
        break;
      case "out-entry-storagePage":
        this.ActiveByCurrentMouth();
        break;
    }
    // 模态框区域滚动
    // this.that.isScroll();
  }
  // 用户选择功能
  getSelectorData(data, _this) {
    data.forEach((item, key, arr) => {
      item.isActive = false;
    });
    _this.isActive = !_this.isActive;
  }

  // 点击重置
  resetPopupData() {
    const type = this.that.type;
    for (let k in this.that.screenData) {
      this.that.screenData[k].forEach(item => {
        if ( item.name != '全部') {
          item.isActive = false;
        };
      });
    }
    switch (type) {
      case "sourcePage":
        this.ActiveByCurrentMouth();
        break;
      case "out-entry-storagePage":
        this.ActiveByCurrentMouth();
        break;
    }
    this.that.startDay = '开始日期';
    this.that.endDay = '结束日期';
    this.that.companyName = '';
  }

  // 点击确定，获取已选所有数据
  confirmData() {
    const type = this.that.type;
    switch (type) {
      case "projectPage":
        this.submitProject_init();
        break;
      case "requirePage":
        this.submitRequire_init();
        break;
      case "sourcePage":
        this.submitSource_init();
        break;
      case "out-entry-storagePage":
        this.submitOutEntry_init();
        break;
    }
  }

  // 初始化当前系统获取月份
  ActiveByCurrentMouth () {
    let currentMonth = getCurMonth();
    let boolean = this.that.screenData.month.some(item => {
      return item.isActive;
    });
    !boolean && this.that.screenData.month.forEach( (item,i)=> {
      if ( item.value == currentMonth ) {
        this.that.upMonthData(i,true);
      }
    });
  }

  // 项目主体入口
  submitProject_init() {
    let screenData = this.that.screenData;
    let _key;
    for (let k in screenData) {
      _key = k;
      screenData[k].filter((item, key, arr) => {
        this.getCommonCurrentType(item, _key, 'projectPage');
      });
      _key = null;
    }
    // 后台发送请求
    let data = ({
      listRows: 15,
      industry: this.industry.join(),
      stage: this.stage.join(),
      datetime: this.datetime.join(),
      companyType: this.companyType.join(),
      type: this._type == 1 ? 1 : 0
    });
    // 发送项目筛选请求
    getVmProjectData({...data}).then(res => {
      let projectInfoArr = res.data.data.data;
      projectInfoArr = projectInfoArr.map((item, key, arr) => {
        switch (item.typeid) {
          case "电力":
            item.iconClass = 'power';
            break;
          case "环保":
            item.iconClass = 'environmental';
            break;
          case "石油石化":
            item.iconClass = 'petrochemical';
            break;
          case '冶金':
            item.iconClass = 'metallurgy';
            break;
          case '化工':
            item.iconClass = 'chemical';
            break;
          case '交通':
            item.iconClass = 'traffic';
            break;
          case '建筑':
            item.iconClass = 'architecture';
            break;
          case "制药":
            item.iconClass = 'pharmacy';
            break;
          case "其他行业":
            item.iconClass = 'other';
            break;
          default:
            item.iconClass = 'other';
            break;
        }
        switch (item.times) {
          case '手续审批':
            item.className = 'examining';
            break;
          case '立项申报':
            item.className = 'entryDeclaration';
            break;
          case '工程设计':
            item.className = 'projectEd';
            break;
        }
        return item;
      });
      if (data.type == 1) { // 单据的项目
        this.that.$store.dispatch("storageBillProjectList", projectInfoArr);
        this.that.$store.dispatch("storagebillSearchHistory", data);
      } else { // 首页列表的项目
        this.that.$store.dispatch("storageProjectList", projectInfoArr);
        this.that.$store.dispatch("storageSearchHistory", data);
      }

      this.that.popupVisible = false;
    });
    this.industry = null;
    this.stage = null;
    this.datetime = null;
    this.companyType = null;
  }

  // 需求主体入口
  submitRequire_init() {
    let screenData = this.that.screenData;
    let companyname = this.that.companyName;
    let startTime = this.that.startDay;
    let endTime = this.that.endDay;
    if (startTime == '开始日期') {
      startTime = null;
    }
    if (endTime == '结束日期') {
      endTime = null;
    }
    let _key;
    for (let k in screenData) {
      _key = k;
      screenData[k].filter((item, key, arr) => {
        this.getCommonCurrentType(item, _key, 'requirePage');
      });
      _key = null;
    }
    let data = {
      listRows: 15,
      type: this.type.join(),
      state: this.state.join(),
      companyType: this.companyType.join(),
      companyname,
      startTime,
      endTime
    };
    // 发送需求筛选请求
    getVmIndexRequireData({...data}).then(res => {
      let requireInfoArr = res.data.data;
      let resArr = requireInfoArr.map((item, key, arr) => {
        switch ( item.iRangeStatus ) {
          case '公告期':
            item.ClassName = 'announcementPeriod';
            break;
          case '投标期':
            item.ClassName = 'tender';
            break;
          case '新建':
            item.ClassName = 'create';
            break;
          case '评标期':
            item.ClassName = 'bidEvaluationPeriod';
            break;
          case '结束期':
            item.ClassName = 'end';
            break;
          case '工程设计':
            item.ClassName = 'ed';
            break;
        }
        switch ( item.cPOType ) {
          case '1':
            item.cPOType = '询比价';
            item.iconClass = 'inquiry';
            break;
          case '2':
            item.cPOType = '竞争性谈判';
            item.iconClass = 'competitiveness';
            break;
          case '3':
            item.cPOType = '单一来源';
            item.iconClass = 'singleSource';
            break;
          case '4':
            item.cPOType = '招投标';
            item.iconClass = 'bid';
            break;
          case '5':
            item.cPOType = '竞价采购';
            item.iconClass = 'competitivePrice';
            break;
          default:
            item.cPOType = '其他';
            item.iconClass = 'other';
            break;
        }
        return item;
      });
      this.that.$store.dispatch("storageRequireList", resArr);
      this.that.$store.dispatch("storageSearchHistory", data);
      this.that.popupVisible = false;
    })
  }

  // 统计分析询源分析主体入口
  async submitSource_init () {
    let screenData = this.that.screenData;
    let key;
    for ( let k in screenData ) {
      key = k;
      screenData[k].forEach((item) => {
        this.getCommonCurrentType(item,key,'sourcePage');
      });
      key = null;
    }
    // 获取开始时间、结束事件
    let month = this.month;
    let startTime = getCountDays(month,1,1);
    let endTime = getCountDays(month,2,1);
    await this.that.$store.dispatch('getSourceMoneyRatio', {startTime, endTime, month});
    this.that.handlerPopup(false);
  }

  // 统计分析出库筛选接口
  async submitOutEntry_init () {
    // 获取开始时间、结束事件
    let screenData = this.that.screenData;
    let _key;
    for( let key in screenData ) {
      _key = key;
      screenData[_key].forEach( (item, index) => {
        this.getCommonCurrentType(item, index, _key, 'out-entry-storagePage');
      });
    }
    let month = this.month ? this.month : getCurMonth();
    let startTime = getCountDays(month,1,1);
    let endTime = getCountDays(month,2,1);
    let warehouse_id = this.outStoreData.join(',');
    let index = this.that.index;
    if (index == 0) {
      await this.that.$store.dispatch('getOut_of_storageData', {startTime, endTime, month, warehouse_id});
    } else if (index == 1) {
      await this.that.$store.dispatch('getWarehousingData', {startTime, endTime, month, warehouse_id});
    }
    this.that.handlerPopup(false);
  }

  // 获取当前选中筛选字段数据
  getCommonCurrentType(item, _key, Type) {
    /*
    *
    * @item -> 当前字段数据
    * @index -> 当前字段数据索引
    * @_key -> 当前数据对象的所有字段
    * @Type -> 筛选弹框的类型
    * */
    switch (Type) {
      // 项目筛选
      case "projectPage":
        // 获取当前选中类型
        if (Object.is(item.isActive, true)) {
          switch (_key) {
            case 'industry':
              this.industry.push(item.dataType);
              break;
            case 'stage':
              this.stage.push(item.dataType);
              break;
            case 'datetime':
              this.datetime.push(item.dataNumber);
              break;
            case 'companyType':
              this.companyType.push(item.dataType);
              break;
          }
        }
        break;
      // 需求筛选页
      case "requirePage":
        if (Object.is(item.isActive, true)) {
          switch (_key) {
            case "type":
              this.type.push(item.dataType);
              break;
            case "state":
              this.state.push(item.dataType);
              break;
            case "companyType":
              this.companyType.push(item.dataType);
              break;
          }
        }
        break;
      // 统计中心、询源分析
      case "sourcePage":
        if ( Object.is(item.isActive, true) ) {
          switch ( _key ) {
            case "month":
              this.month = item.value;
              break;
          }
        }
        break;
      // 出库仓库筛选
      case "out-entry-storagePage":
        switch (_key) {
          case "warehouse":
            if ( index == this.tempoutStoreIndex ) {
              this.outStoreData.push(item.id);
            }
          break;
        }
        break;
    }
  }

}
